 package  
{
	import flash.display.GraphicsBitmapFill;
	import flash.events.Event;
	import org.flixel.FlxState;
	import org.flixel.FlxSprite;
	import org.flixel.*;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	/**
	 * ...
	 * @author ...
	 */
	public class PlayState extends FlxState
	{
		// Art
		//[Embed(source = '../lib/WhiteLightCore2.png')] protected var BallPic:Class;
		[Embed(source='../lib/background02.jpg')] protected var BGPic:Class;
		[Embed(source = '/org/flixel/data/cursor.png')] protected var cursorPic:Class;
		//[Embed(source = '../lib/crystal.png')] protected var crystalPic:Class;
		//[Embed(source = '../lib/crystal_lit.png')] protected var crystalPicLit:Class;
		//[Embed(source = '../lib/crystal_dark.png')] protected var crystalPicDark:Class;
		[Embed(source = '../lib/FullCrystal.png')] protected var crystalFull:Class;
		[Embed(source = '../lib/WhiteLightCore_smallest.png')] protected var powerPic:Class;
		
		// Sounds
		//[Embed(source = '../lib/Sounds/ggj11_light_Loop1.wav', mimeType = 'application/octet-stream')] protected var lightSound1:Class;
		//[Embed(source = '../lib/Sounds/ggj11_light_Loop2.wav', mimeType = 'application/octet-stream')] protected var lightSound2:Class;
		//[Embed(source = '../lib/Sounds/ggj11_light_Loop3.wav', mimeType = 'application/octet-stream')] protected var lightSound3:Class;
		[Embed(source = '../lib/Sounds/acid_cut.mp3')] protected var backTrack:Class;
		
		private var lBall:LightBall = null;
		private var darknessController:DarknessController;
		private var lCrystal:LightCrystal;
		private var lPowers:FlxGroup;
		private var spawnCounter:Number;
		private var lightShockwaves:FlxGroup;
		private var deathBuffer:Number = 0;
		private var crystalDangerZone:CrystalDangerZone;
		private var deathTimer:Timer;
		private var lSound:FlxSound;
		
		override public function create():void
		{
			this.add(new FlxSprite(0,0,BGPic));
			
			lBall = new LightBall(385, 350);
			this.add(lBall);
			//lSound = FlxG.play(lightSound1, 1.0, true);
			
			lCrystal = new LightCrystal (389, 225, crystalFull);
			this.add(lCrystal);
			
			crystalDangerZone = new CrystalDangerZone(lCrystal);
			this.add(crystalDangerZone);
			
			darknessController = new DarknessController();
			darknessController.lightCrystal = lCrystal;
			this.add(darknessController);
			
			lightShockwaves = new FlxGroup();
			this.add(lightShockwaves);
			
			lPowers = new FlxGroup();
			this.add(lPowers);
			
			FlxG.mouse.show(cursorPic);
			super.create();
			
			spawnCounter = 50;

			FlxG.playMusic(backTrack, .75);
			
			deathTimer = new Timer(2000, 1);
			deathTimer.addEventListener(TimerEvent.TIMER_COMPLETE, crystalDeath);
		}
		
		override public function update():void
		{
			FlxU.overlap(lBall, lPowers, overlapBallPower);
			FlxU.overlap(lBall, lCrystal, overlapBallCrystal);
			FlxU.overlap(lBall, darknessController, overlapBallDarkness); //overlapBallDarkness);
			FlxU.overlap(lightShockwaves, darknessController, overlapShockwaveDarkness);
			FlxU.overlap(crystalDangerZone, darknessController, overlapCDZDarkness);
			
			if (spawnCounter < 0)
			{
				spawnPower();
				spawnCounter = 40;
				//FlxG.log("Spawn happened");
			}
			
			spawnCounter--;
			
			if (deathBuffer > 0)
				deathBuffer--;
			
			if (lCrystal.health <= 0)
			{
				FlxG.timeScale = .1;
				deathTimer.start();
			}
				
			super.update();
		}
	
		private function spawnPower():void
		{
			var power:LightPower = new LightPower(Math.random() * FlxG.width, Math.random() * FlxG.height, powerPic);
			//var power:LightPower = new LightPower(100, 150, powerPic);
			lPowers.add(power);
		}	
	
		public function crystalDeath(event:TimerEvent):void
		{
			FlxG.state = new TitleState();
		}
		
		private function overlapBallPower(ball:LightBall, power:LightPower):void
		{
			power.kill();
			ball.increasePowerCount();
		}
		
		private function overlapBallCrystal(ball:LightBall, crystal:LightCrystal):void
		{
			//FlxG.log("COLLIDE!");
			
			if (!crystal.isLitUp() && ball.enoughPower()) {
				for each(var dark:DarknessBlob in darknessController.members)
				{
					dark.CrystalExplodeDie(crystal);
				}
				crystal.lightUp();
				ball.decreasePowerCount();
				add(new LightShockwave(crystal.x + crystal.origin.x, crystal.y + crystal.origin.y, 175));
			}
				
		}
		
		private function overlapShockwaveDarkness(shockwave:LightShockwave, darkness:DarknessBlob):void
		{
			FlxG.log("Hit!");
			darkness.RunFromLight(shockwave);
		}
		
		private function overlapBallDarkness(ball:LightBall, darknesses:DarknessBlob):void
		{
			var numP:Number = ball.numPower();
			
			if (numP > 0){
				lightShockwaves.add(new LightShockwave(ball.x, ball.y, 10 * numP));
				ball.decreasePowerCount();
				FlxG.log("explode");
				deathBuffer = 20;
			}
			else if(deathBuffer <= 0){
				ball.kill();
				//lSound.stop();
			}
		}
		
		private function overlapCDZDarkness(zone:CrystalDangerZone, darkness:DarknessBlob):void
		{
			
			if(darkness.Attack(zone.lightCrystal));
			{
				zone.lightCrystal.TakeDamage(1);
				var halfWayX:int =  zone.x + zone.origin.x + (((darkness.x + darkness.origin.x) - zone.x + zone.origin.x)/2);
				var halfWayY:int =  zone.y + zone.origin.y + (((darkness.y + darkness.origin.y) - zone.y + zone.origin.y)/2);
				add(new AttackFX(halfWayX, halfWayY, 60, "Aw snapz!")); 
			}
		}
	}
}